<?php

namespace support;

class UploadFile extends File
{
    protected $originalName;
    protected $mimeType;
    protected $error;

    public function __construct(string $path, string $originalName, string $mimeType, int $error)
    {
        $this->originalName = $originalName;
        $this->mimeType     = $mimeType ?: 'application/octet-stream';
        $this->error        = $error ?: UPLOAD_ERR_OK;
        parent::__construct($path);
    }

    public function isValid()
    {
        return $this->error === UPLOAD_ERR_OK;
    }

    public function getOriginalName()
    {
        return $this->originalName;
    }

    public function getMimeType()
    {
        return $this->mimeType;
    }

    public function getError()
    {
        return $this->error;
    }

    public function getErrorMessage()
    {
        $message = '';
        switch ($this->error) {
            case 0:
                $message = '';
                break;
            case 1:
            case 2:
                $message = 'upload File size exceeds the maximum value';
                break;
            case 3:
                $message = 'only the portion of file is uploaded';
                break;
            case 4:
                $message = 'no file to uploaded';
                break;
            case 6:
                $message = 'upload temp dir not found';
                break;
            case 7:
                $message = 'file write error';
                break;
            default:
                $message = 'unknown upload error';
        }

        return $message;
    }

    public function getExtension(): string
    {
        $ext = parent::getExtension();
        if (!$ext) {
            $ext = pathinfo($this->getOriginalName(), PATHINFO_EXTENSION);;
        }
        return strtolower($ext);
    }
}
